#include "gameengine.h"
#include <vector>
#include <string>
#include "SelectDialog.h"
#include <cstring>
#include <stdlib.h>
GameEngine::GameEngine() {
    whiteIsComputer = false;
    blackIsComputer = false;
    didPawnPromote = false;
}
GameEngine::~GameEngine() {
    myBoard.removeAllPieces();
}
//Called by on_LoadView
void GameEngine::loadView(IChessView * view) {
    myView = view;
    myBoard.setView(view);
    myXML.setView(view,&myBoard);
    myAI.setView(view,&myBoard,this);
}
//Called by on_TimerEvent
void GameEngine::onTimer() {
    if(turn==white && whiteIsComputer) {
        doRandomComputerMove();
        return;
    }
    if(turn==black && blackIsComputer) {
        doRandomComputerMove();
        return;
    }
}
//Called by on_UndoMove
void GameEngine::onUndo() {
    if(myBoard.moveHistory.size()==0 || moving) {
        return;
    }
    if(myBoard.undoOneMove()) {
        changeTurn();
    }
    if(!gameStarted) {
        gameStarted = true;
    }
}
//called by on_DragStart()
void GameEngine::onDragStart(int row , int col) {
    cellSelect(row,col);
}
//Called by on_CellSelect
void GameEngine::cellSelect(int row, int col) {
    //myBoard.printMoveHistory();
    if(!gameStarted) {
        return;
    }
    if((whiteIsComputer && turn==white) || (blackIsComputer && turn==black)) {
        return;
    }
    myBoard.unhighlightAll();
    if(previousCell.col == 10) {
        if(TurnCheck(row,col)) {
            myBoard.highlightCell(row,col);
            previousCell.setCell(row,col);
        } else {
            return;
        }
    } else {
        if(doSelectPiece(row,col)) {
            return;
        }
    }
    MakePossibleMoves(row,col);
}
void GameEngine::cellSelectComp(int row, int col) {
    if(!gameStarted) {
        return;
    }
    myBoard.unhighlightAll();
    if(previousCell.col == 10) {
        if(TurnCheck(row,col)) {
            myBoard.highlightCell(row,col);
            previousCell.setCell(row,col);
        } else {
            return;
        }
    } else {
        if(doSelectPiece(row,col)) {
            return;
        }
    }
    MakePossibleMoves(row,col);
}
void GameEngine::PromotePawn(int row , int col , Color cor , signed int pieceType) {
    myBoard.KillPiece(row,col);
    ChessPiece * toAdd = NULL;
    switch(pieceType) {
    case (1):
        toAdd = new Queen(row,col,cor);
        break;
    case (2):
        toAdd = new Bishop(row,col,cor);
        break;
    case (3):
        toAdd = new Knight(row,col,cor);
        break;
    case (4):
        toAdd = new Rook(row,col,cor);
        break;
    default:
        toAdd = new Queen(row,col,cor);
    }
    toAdd->setBoard(&myBoard);
    myBoard.piecesOnBoard.push_back(toAdd);
    myBoard.PlacePiece(toAdd);
    didPawnPromote = true;
}
void GameEngine::CheckPawnPromote(int row , int col) {
    ChessPiece * p = myBoard.getPieceAt(row,col);
    const std::string title("Pawn Promotion");
    const std::string message("Choose your Piece");
    std::vector <std::string> labels;
    labels.resize(4);
    labels[0]="Queen";
    labels[1]="Bishop";
    labels[2]="Knight";
    labels[3]="Rook";
    if(p->image == W_PAWN && p->row==0) {
        //promote white pawn
        signed int pieceType = 1;
        if(!whiteIsComputer) {
            pieceType = SelectDialog::SelectDialogRun(title,message,labels);
        }
        PromotePawn(row,col,white,pieceType);
        return;
    }
    if(p->image == B_PAWN && p->row==7) {
        //promote black pawn
        signed int pieceType = 1;
        if(!blackIsComputer) {
            pieceType = SelectDialog::SelectDialogRun(title,message,labels);
        }
        PromotePawn(row,col,black,pieceType);
    }
}
void GameEngine::CheckCastlingMoves(int row , int col) {
    if(row == 0 && col == 0)
        myBoard.blackLRookMoved = true;
    if(row == 0 && col == 7)
        myBoard.blackRRookMoved = true;
    if(row == 0 && col == 4)
        myBoard.blackKingMoved = true;
    if(row == 7 && col == 0)
        myBoard.whiteLRookMoved = true;
    if(row == 7 && col == 7)
        myBoard.whiteRRookMoved = true;
    if(row == 7 && col == 4)
        myBoard.whiteKingMoved = true;
}
bool GameEngine::CheckIsCastle(int row , int col) {
    ChessPiece * p = myBoard.getPieceAt(row,col);
    bool ret = false;
    if(!myBoard.whiteKingMoved && p->image == W_KING) {
        if(row == 7 && col == 6 && !myBoard.whiteRRookMoved) {
            myBoard.movePiece(7,7,7,5);
            //myView->WriteMessageArea("DID CASTLE!\n");
            ret = true;
        }
        if(row == 7 && col == 2 && !myBoard.whiteLRookMoved) {
            myBoard.movePiece(7,0,7,3);
            //myView->WriteMessageArea("DID CASTLE!\n");
            ret = true;
        }
    }
    if(!myBoard.blackKingMoved && p->image == B_KING) {
        if(row == 0 && col == 6 && !myBoard.blackRRookMoved) {
            myBoard.movePiece(0,7,0,5);
            //myView->WriteMessageArea("DID CASTLE!\n");
            ret = true;
        }
        if(row == 0 && col == 2 && !myBoard.blackLRookMoved) {
            myBoard.movePiece(0,0,0,3);
            //myView->WriteMessageArea("DID CASTLE!\n");
            ret = true;
        }
    }
    return ret;
}
void GameEngine::CheckEnPassantSetup(int previousRow , int row , int col) {
    myBoard.lastWasDoublePawn = false;
    myBoard.lastDoublePawn = Cell(888,888);
    myBoard.canDoEnPassant = false;
    if(myBoard.getPieceAt(row,col)->image == W_PAWN) {
        if(previousRow == 6 && row == 4) {
            myBoard.lastWasDoublePawn = true;
            myBoard.lastDoublePawn = Cell(row,col);
        }
    }
    if(myBoard.getPieceAt(row,col)->image == B_PAWN) {
        if(previousRow == 1 && row == 3) {
            myBoard.lastWasDoublePawn = true;
            myBoard.lastDoublePawn = Cell(row,col);
        }
    }
}
bool GameEngine::CheckIsEnPassant(int row, int col) {
    if(myBoard.getPieceAt(row,col)->image == B_PAWN && myBoard.canDoEnPassant &&
            myBoard.lastDoublePawn.col == col && row == 5) {
        myBoard.KillPiece(row-1,col);
        //myView->WriteMessageArea("DID EN PASSANT!\n");
        return true;
    }
    if(myBoard.getPieceAt(row,col)->image == W_PAWN && myBoard.canDoEnPassant &&
            myBoard.lastDoublePawn.col == col && row == 2) {
        myBoard.KillPiece(row+1,col);
        //myView->WriteMessageArea("DID EN PASSANT!\n");
        return true;
    }
    return false;
}
Move GameEngine::makeMove(int prow , int pcol , int row , int col) {
    ChessPiece * prev = myBoard.getPieceAt(prow,pcol);
    ChessPiece * toKill = myBoard.getPieceAt(row,col);
    return Move(prev,toKill,row,col);
}
bool GameEngine::doSelectPiece(int row , int col) {
    myBoard.unhighlightCell(previousCell);
    if(isAMove(row,col)) {
        Move toAdd = makeMove(previousCell.row,previousCell.col,row,col);
        myBoard.movePiece(previousCell.row , previousCell.col , row, col);
        possibleMoves.clear();
        CheckPawnPromote(row,col);
        if(didPawnPromote) {
            //change the move history
            toAdd.didPromote(myBoard.getPieceAt(row,col));
            didPawnPromote = false;
        }
        toAdd.castle = CheckIsCastle(row,col);
        CheckCastlingMoves(row,col);
        CheckCastlingMoves(previousCell.row,previousCell.col);
        toAdd.enPassant = CheckIsEnPassant(row,col);
        CheckEnPassantSetup(previousCell.row,row,col);
        myBoard.moveHistory.push_back(toAdd);
        changeTurn();
        CheckCheck(row,col);
        if(myBoard.blackIsInCheckmate || myBoard.whiteIsInCheckmate || myBoard.stalemate) {
            doEndGame();
        }
        return true;
    } else {
        if(TurnCheck(row,col)) {
            myBoard.highlightCell(row,col);
        } else {
            return true;
        }
    }
    previousCell.setCell(row,col);
    return false;
}
bool GameEngine::doEndGame() {
    if(myBoard.whiteIsInCheckmate)
        myView->WriteMessageArea("BLACK WINS!!!!!");
    if(myBoard.blackIsInCheckmate)
        myView->WriteMessageArea("WHITE WINS!!!!!");
    gameStarted = false;
}
bool GameEngine::TurnCheck(int row , int col) {
    ChessPiece * p = myBoard.getPieceAt(row , col);
    if(p!=NULL)
        return turn==p->pColor;
    return true;
}
void GameEngine::changeTurn() {
    turn = (turn==white) ? black : white;
    std::string topStatus = (turn==white) ? "" : "Black's turn!";
    std::string bottomStatus = (turn==black) ? "" : "White's turn!";
    std::string message = "It's ";
    message += (turn==white) ? "White's " : "Black's ";
    message += "turn.\n";
    //myView->WriteMessageArea(message);
    myView->SetBottomLabel(bottomStatus);
    myView->SetTopLabel(topStatus);
}
//called by on_startNewGame
void GameEngine::startNewGame(int gameArgs) {
    switch(gameArgs) {
    case(1):
        whiteIsComputer = false;
        blackIsComputer = false;
        StartAGame();
        break;
    case(2):
        whiteIsComputer = true;
        blackIsComputer = false;
        StartAGame();
        break;
    case(3):
        whiteIsComputer = false;
        blackIsComputer = true;
        StartAGame();
        break;
    case(4):
        whiteIsComputer = true;
        blackIsComputer = true;
        StartAGame();
        break;
    case(5):
    default:
        StartNormalGame();
    }
}
//called by on_LoadGame
void GameEngine::loadGame() {
    std::string fileToLoad = myView->SelectLoadFile();
    if(fileToLoad.length()!=0) {
        const std::string title("Load Game!");
        const std::string message("Are you sure you want to load " + fileToLoad);
        std::vector <std::string> labels;
        labels.resize(2);
        labels[0]="Yes";
        labels[1]="No";
        signed int willLoad = SelectDialog::SelectDialogRun(title,message,labels);
        if(willLoad==1) {
            myView->ClearMessageArea();
            myBoard.removeAllPieces();
            myBoard.ClearAllPieces();
            saveLocation = "";
            myBoard.unhighlightAll();
            gameStarted = true;
            //do
            myBoard.whiteIsInCheck = false;
            myBoard.whiteIsInCheckmate = false;
            myBoard.blackIsInCheck = false;
            myBoard.blackIsInCheckmate = false;
            myBoard.stalemate = false;
            myBoard.whiteKingMoved = false;
            myBoard.whiteRRookMoved = false;
            myBoard.whiteLRookMoved = false;
            myBoard.blackKingMoved = false;
            myBoard.blackRRookMoved = false;
            myBoard.blackLRookMoved = false;
            myBoard.lastWasDoublePawn = false;
            myBoard.lastDoublePawn = Cell(123,123);
            myBoard.willDoCastle = false;
            myBoard.canDoEnPassant = false;
            moving = false;
            myBoard.moveHistory.clear();
            clicked = false;
            myXML.loadGame(fileToLoad);
            //take loaded <ChessPiece*> and other stuff and copy it here
            myBoard.checkLoadedCastleMoves();
            myBoard.checkLastWasDoublepawn();
            turn = (myBoard.moveHistory.size() % 2 == 0) ? black : white;
            changeTurn();
            myBoard.PlaceAllPieces();
            CheckCheck(3,3);
            myView->SetStatusBar("LOADED: " + fileToLoad);
        }
    }
}
//called by on_SaveGame
void GameEngine::saveGame() {
    if(saveLocation.length()!=0) {
        myXML.saveGame(saveLocation);
    } else {
        saveGameAs();
    }
}
//called by on_SaveGameAs
void GameEngine::saveGameAs() {
    gameStarted = false;
    saveLocation = myView->SelectSaveFile();
    if(saveLocation.length()!=0) {
        const std::string title("Save Game!");
        const std::string message("Are you sure you want to save to " + saveLocation);
        std::vector <std::string> labels;
        labels.resize(2);
        labels[0]="Yes";
        labels[1]="No";
        signed int willLoad = SelectDialog::SelectDialogRun(title,message,labels);
        if(willLoad==1) {
            myXML.saveGameAs(saveLocation);
            myView->SetStatusBar("SAVED: " + saveLocation);
        } else {
            saveLocation = "";
        }
    }
    gameStarted = true;
}
void GameEngine::StartAGame() {
    myView->ClearMessageArea();
    myBoard.removeAllPieces();
    myBoard.ClearAllPieces();
    saveLocation = "";
    StartFreshGame();
    myBoard.unhighlightAll();
    gameStarted = true;
    myBoard.whiteIsInCheck = false;
    myBoard.whiteIsInCheckmate = false;
    myBoard.blackIsInCheck = false;
    myBoard.blackIsInCheckmate = false;
    myBoard.stalemate = false;
    myBoard.whiteKingMoved = false;
    myBoard.whiteRRookMoved = false;
    myBoard.whiteLRookMoved = false;
    myBoard.blackKingMoved = false;
    myBoard.blackRRookMoved = false;
    myBoard.blackLRookMoved = false;
    myBoard.lastWasDoublePawn = false;
    myBoard.willDoCastle = false;
    myBoard.canDoEnPassant = false;
    moving = false;
    myBoard.lastDoublePawn = Cell(123,123);
    myBoard.moveHistory.clear();
    clicked = false;
    turn = black;
    changeTurn();
}
void GameEngine::StartNormalGame() {
    std::string aMessage = "";
    int gameType = ChooseNewGameType();
    switch(gameType) {
    case 1:    {
        aMessage = "White goes first.\nGood Luck!";
        whiteIsComputer = false;
        blackIsComputer = false;
        StartAGame();
        break;
    }
    case 2:    {
        const std::string title("New Game");
        const std::string message("Choose your Color");
        std::vector <std::string> labels;
        labels.resize(2);
        labels[0]="White";
        labels[1]="Black";
        signed int humanColor = SelectDialog::SelectDialogRun(title,message,labels);
        aMessage += "You are ";
        aMessage += (humanColor == 1) ? "White" : "Black";
        aMessage += ".\nWhite goes first.";
        switch(humanColor) {
        case(1):
            whiteIsComputer = false;
            blackIsComputer = true;
            StartAGame();
            break;
        default:
            whiteIsComputer = true;
            blackIsComputer = false;
            StartAGame();
        }
        break;
    }
    case 3:    {
        whiteIsComputer = true;
        blackIsComputer = true;
        StartAGame();
        aMessage = "enjoy.";
        break;
    }
    default:
        aMessage = "what did you do?";
        StartNormalGame();
    }
}
void GameEngine::StartFreshGame() {
    myBoard.removeAllPieces();
    myBoard.piecesOnBoard.push_back(new Pawn(1,0,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,1,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,2,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,3,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,4,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,5,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,6,black));
    myBoard.piecesOnBoard.push_back(new Pawn(1,7,black));
    myBoard.piecesOnBoard.push_back(new Pawn(6,0,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,1,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,2,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,3,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,4,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,5,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,6,white));
    myBoard.piecesOnBoard.push_back(new Pawn(6,7,white));
    AddOtherPieces();
    myBoard.PlaceAllPieces();
    previousCell.setCell(10,10);
    SetPiecesToBoard();
}
void GameEngine::SetPiecesToBoard() {
    std::vector<ChessPiece *>::iterator it;
    for ( it=myBoard.piecesOnBoard.begin() ; it < myBoard.piecesOnBoard.end(); it++ ) {
        ChessPiece * p = *it;
        p->setBoard(&myBoard);
    }
}
void GameEngine::AddOtherPieces() {
    myBoard.piecesOnBoard.push_back(new Rook(0,0,black));
    myBoard.piecesOnBoard.push_back(new Knight(0,1,black));
    myBoard.piecesOnBoard.push_back(new Bishop(0,2,black));
    myBoard.piecesOnBoard.push_back(new Queen(0,3,black));
    myBoard.piecesOnBoard.push_back(new King(0,4,black));
    myBoard.piecesOnBoard.push_back(new Bishop(0,5,black));
    myBoard.piecesOnBoard.push_back(new Knight(0,6,black));
    myBoard.piecesOnBoard.push_back(new Rook(0,7,black));
    myBoard.piecesOnBoard.push_back(new Rook(7,0,white));
    myBoard.piecesOnBoard.push_back(new Knight(7,1,white));
    myBoard.piecesOnBoard.push_back(new Bishop(7,2,white));
    myBoard.piecesOnBoard.push_back(new Queen(7,3,white));
    myBoard.piecesOnBoard.push_back(new King(7,4,white));
    myBoard.piecesOnBoard.push_back(new Bishop(7,5,white));
    myBoard.piecesOnBoard.push_back(new Knight(7,6,white));
    myBoard.piecesOnBoard.push_back(new Rook(7,7,white));
}
int GameEngine::ChooseNewGameType() {
    const std::string title("New Game!");
    const std::string message("Choose Type Of Game.");
    std::vector <std::string> labels;
    labels.resize(3);
    labels[0]="Human vs Human";
    labels[1]="Human vs Computer";
    labels[2]="Computer vs Computer";
    int gameType = SelectDialog::SelectDialogRun(title,message,labels);
    return gameType;
}
bool GameEngine::isAMove(int row , int col) {
    std::vector<Cell>::iterator it;
    for ( it=possibleMoves.begin() ; it < possibleMoves.end(); it++ ) {
        Cell c = *it;
        if(c.row == row && c.col == col)
            return true;
    }
    return false;
}
void GameEngine::FindMoves(std::vector<Cell> & myMoves , ChessPiece * p) {
    p->GetAllPossible(myMoves);
    //myMoves now has all of the pieces base moves
    //-------------------------------------------------------
    std::vector<Cell> removeThese;
    //put cool check check stuff here.
    Cell startLoc = Cell(p->row , p->col);
    std::vector<Cell>::iterator it;
    for ( it =  myMoves.begin() ; it < myMoves.end() ; it++) {
        //for each move in possible moves
        Cell currentMove = *it;
        // move it there
        //--------------------DO THIS BETTER!!----------------
        //if there is a piece that will theoretically be killed, save it's piece as a pointer
        ChessPiece * killedPiece = NULL;
        if(myBoard.hasEnemy(p->pColor,currentMove.row , currentMove.col)) {
            killedPiece = myBoard.getPieceAt(currentMove.row , currentMove.col);
            killedPiece->setCell(100,100);
        }
        //-------------------DO ABOVE BETTER!---------------------
        myBoard.moveTestPiece(startLoc.row,startLoc.col,currentMove.row,currentMove.col);
        //check if any enemy piece has a possible move on the team king
        //first, find team king position
        Cell TeamKing = myBoard.findTeamKing(p);
        //then, get all enemy possible moves
        std::vector<Cell> allEnemyMoves;
        std::vector<ChessPiece*>::iterator it2;
        for( it2 = myBoard.piecesOnBoard.begin() ; it2 < myBoard.piecesOnBoard.end() ; it2++) {
            //for each piece on the board
            ChessPiece * p2 = *it2;
            if(myBoard.hasEnemy(p->pColor,p2->row,p2->col)) {
                //if the piece is an enemy
                p2->GetAllPossible(allEnemyMoves);
                //add the possible moves to all enemy moves.
            }
        }
        //check if any possible move contains the king.
        std::vector<Cell>::iterator it3;
        for ( it3 = allEnemyMoves.begin() ; it3 < allEnemyMoves.end() ; it3++) {
            Cell checkThisKillCell = *it3;
            if(checkThisKillCell.row == TeamKing.row && checkThisKillCell.col == TeamKing.col) {
                //THIS POSSIBLE MOVE WILL PUT THE TEAM"S KING IN CHECK.
                //ERASE THIS CELL FROM MYMOVES!!!!
                removeThese.push_back(Cell(currentMove.row,currentMove.col));
            }
        }
        //put the board back like it started
        myBoard.moveTestPiece(currentMove.row,currentMove.col , startLoc.row, startLoc.col);
        //------------------------AGAIN, DO THIS BETTER------------
        if(killedPiece!=NULL) {
            //there was a kill
            killedPiece->setCell(currentMove.row,currentMove.col);
        }
        //-------------------------DO ABOVE BETTER------------
    }
    //now that you have found any cell that contains checks, remove them from myMoves
    RemoveFromPossible(myMoves , removeThese);
    //------------------------------------------------------
}
void GameEngine::CheckCheckmate(std::vector<Cell> possBlackMoves,
                                std::vector<Cell> possWhiteMoves) {
    myBoard.blackIsInCheckmate = false;
    myBoard.whiteIsInCheckmate = false;
    if(possBlackMoves.size()==0) {
        if(myBoard.blackIsInCheck) {
            myBoard.blackIsInCheckmate = true;
            myView->WriteMessageArea("BLACK IS IN CHECKMATE\n");
        } else {
            myView->WriteMessageArea("STALEMATE, NO ONE WINS");
            myBoard.stalemate = true;
        }
    }
    if(possWhiteMoves.size()==0) {
        if(myBoard.whiteIsInCheck) {
            myBoard.whiteIsInCheckmate = true;
            myView->WriteMessageArea("WHITE IS IN CHECKMATE\n");
        } else {
            myView->WriteMessageArea("STALEMATE, NO ONE WINS");
            myBoard.stalemate = true;
        }
    }
}
void GameEngine::AddToMoves(std::vector<Cell> & addTo , std::vector<Cell> addFrom) {
    std::vector<Cell>::iterator it;
    for ( it = addFrom.begin() ; it < addFrom.end() ; it++) {
        Cell c = *it;
        addTo.push_back(c);
    }
}
void GameEngine::CheckCheck(int row , int col) {
    Cell whiteKing = myBoard.findKing(white);
    Cell blackKing = myBoard.findKing(black);
    std::vector<Cell> possBlackMoves;
    std::vector<Cell> possWhiteMoves;
    std::vector<ChessPiece*>::iterator it;
    for(it = myBoard.piecesOnBoard.begin() ; it < myBoard.piecesOnBoard.end() ; it++) {
        ChessPiece * p = *it;
        std::vector<Cell> possMoves;
        if(p->pColor == black) {
            FindMoves(possMoves,p);
            AddToMoves(possBlackMoves,possMoves);
        } else {
            FindMoves(possMoves,p);
            AddToMoves(possWhiteMoves,possMoves);
        }
    }
    bool blackCheck = false;
    bool whiteCheck = false;
    std::vector<Cell>::iterator it2;
    for( it2 = possBlackMoves.begin() ; it2 < possBlackMoves.end() ; it2++) {
        Cell checkForWhiteKing = *it2;
        if(checkForWhiteKing.row == whiteKing.row && checkForWhiteKing.col == whiteKing.col) {
            whiteCheck = true;
        }
    }
    for( it2 = possWhiteMoves.begin() ; it2 < possWhiteMoves.end() ; it2++) {
        Cell checkForBlackKing = *it2;
        if(checkForBlackKing.row == blackKing.row && checkForBlackKing.col == blackKing.col) {
            blackCheck = true;
        }
    }
    CheckColors(blackCheck,whiteCheck);
    /*
     myView->WriteMessageArea("black has " +
                              MyFunctions::convertInt(possBlackMoves.size()) +
                              " moves left\n");
     myView->WriteMessageArea("white has " +
                              MyFunctions::convertInt(possWhiteMoves.size()) +
                              " moves left\n");
 */
    CheckCheckmate(possBlackMoves,possWhiteMoves);
}
void GameEngine::CheckColors(bool blackCheck, bool whiteCheck) {
    if(blackCheck) {
        myBoard.blackIsInCheck = true;
        myView->WriteMessageArea("Black is in check\n");
        myView->SetTopLabel("Black's turn! : BLACK IS IN CHECK!");
    } else {
        myBoard.blackIsInCheck = false;
        //myView->WriteMessageArea("Black is NOT in check\n");
    }
    if(whiteCheck) {
        myBoard.whiteIsInCheck = true;
        myView->WriteMessageArea("White is in check\n");
        myView->SetBottomLabel("White's turn! : WHITE IS IN CHECK!");
    } else {
        myBoard.whiteIsInCheck = false;
        //myView->WriteMessageArea("White is NOT in check\n");
    }
}
void GameEngine::RemoveFromPossible(std::vector<Cell> & moves , std::vector<Cell> & badCells) {
    for ( int i = 0 ; i < badCells.size() ; i++) {
        Cell bad = badCells[i];
        std::vector<Cell>::iterator it;
        for ( it = moves.begin() ; it < moves.end() ; it++) {
            Cell goodCheck = *it;
            if(goodCheck.row == bad.row && goodCheck.col == bad.col) {
                moves.erase(it);
                break;
            }
        }
    }
}
void GameEngine::MakePossibleMoves(int row , int col) {
    int PiecePosition = myBoard.getPiecePosAt(row,col);
    if(PiecePosition!=200) {
        ChessPiece * p = myBoard.piecesOnBoard[PiecePosition];
        if(p->row != 10) {
            std::vector <Cell> AllPossibleMoves;
            FindMoves(AllPossibleMoves,p);
            HighlightCells(AllPossibleMoves);
            possibleMoves.clear();
            possibleMoves = AllPossibleMoves;
        } else {
            //myView->WriteMessageArea("you clicked a blank square\n");
        }
    }
}
void GameEngine::HighlightCells(std::vector<Cell> & cells) {
    std::vector<Cell>::iterator it;
    for ( it=cells.begin() ; it < cells.end(); it++ ) {
        Cell c = *it;
        if(myBoard.getPieceAt(c.row , c.col)!=NULL) {
            myView->HighlightSquare(c.row , c.col , RED_SQUARE);
        } else {
            myView->HighlightSquare(c.row , c.col , GREEN_SQUARE);
        }
    }
}
void GameEngine::doRandomComputerMove() {
    myAI.doRandomComputerMove();
}
